home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 4 / United Public Domain Gold 4.iso / scope / sc093.dms / sc093.adf / LM / lm2.c < prev    next >
C/C++ Source or Header  |  1989-12-02  |  7KB  |  394 lines

  1. #include    <stdio.h>
  2. #include    "lm.h"
  3.  
  4. extern    long    in_file, out_file;
  5. extern    char    *get_next_file(), *in_file_name;
  6. extern    BOOL    same_output_file;
  7.  
  8. struct symbol    *sym_list_head, *sym_ptr;
  9.  
  10. extern long    mod_buf[MBUF_SIZE];
  11. int        eolib, symbol_offset;
  12.  
  13. unsigned long    buf_count, old_buf_count, current_long, temp;
  14. void        *AllocMem();
  15.  
  16. long        Open(), Write();
  17.  
  18.  
  19. /* --------------------------------------------------------------------------
  20.             STRIP_LIBRARY ROUTINES
  21. ---------------------------------------------------------------------------*/
  22.  
  23. strip_library()
  24.     {
  25.     /* 
  26.       Read through the library and discard the symbol and debug
  27.       hunks.
  28.     */    
  29.     unsigned    module_size;
  30.     unsigned    long end_o_hunk = HUNK_END;
  31.  
  32.     printf( ">> Stripping: <%s>\n",in_file_name );
  33.  
  34.     while( (module_size = read_lib_module() ) )
  35.         {
  36.         /*
  37.           Strip symbol/debug hunks if there. All we do is write
  38.           back the module up to the symbol/debug hunk and stick
  39.           a HUNK_END on the end.
  40.         */
  41.         if ( symbol_offset )
  42.             {
  43.             Write( out_file, mod_buf, (long) (symbol_offset << 2) );
  44.             Write( out_file, &end_o_hunk, 4L );
  45.             }
  46.  
  47.         else
  48.             {
  49.             Write( out_file, mod_buf, (long) (module_size << 2) );
  50.             }
  51.         }
  52.     }
  53.  
  54.  
  55.  
  56.  
  57. /* --------------------------------------------------------------------------
  58.             ADD_MODULE ROUTINES
  59. ---------------------------------------------------------------------------*/
  60.  
  61. add_modules( file_list )
  62. char    *file_list;
  63.  
  64.     {
  65.     char        *p;
  66.     long        fp;
  67.     unsigned long    longword;
  68.  
  69.     copy_lib( in_file, out_file );
  70.  
  71.     while( (p = get_next_file( file_list ) ) )
  72.         {
  73.         
  74.         if ( !(fp = Open( p, MODE_OLDFILE ) ) )
  75.             {
  76.             printf( "ERROR: Object file: <%s> not found\n", p );
  77.             }
  78.         else
  79.             {
  80.             printf( ">> Adding: <%s>\n", p );
  81.             copy_lib( fp, out_file );
  82.             Close( fp );
  83.             }
  84.         }
  85.     }    
  86.  
  87.  
  88.  
  89.  
  90. add_symbol( sym_name ) 
  91. char    *sym_name;
  92.     {
  93.     /* 
  94.       Add the symbol in sym_name to the list of symbols.
  95.     */
  96.  
  97.     struct symbol    *p;
  98.  
  99.     if(!(p=(struct symbol*)AllocMem((long)sizeof(struct symbol), 0L)))
  100.         {
  101.         my_exit( ENOMEM );
  102.         }
  103.  
  104.     p->next = NULL;
  105.     sym_ptr->next = p;
  106.     sym_ptr = p;
  107.  
  108.     strcpy( sym_ptr->name, sym_name );
  109.     }
  110.     
  111.  
  112.  
  113. get_and_put()
  114.     {
  115.     /* 
  116.       Fetch a longword from the input file, stick it in the module
  117.       buffer, and return it. Return NULL if no more to read.
  118.     */
  119.     long    longword;
  120.  
  121.     /*
  122.       First, make sure we are not overwriting innocent ram!
  123.     */
  124.     if ( buf_count > MBUF_SIZE )
  125.         {
  126.         my_exit( ENOBUF );
  127.         }
  128.  
  129.  
  130.     if ( eolib )        /* End of library? */
  131.         {
  132.         my_exit( EOLIB );
  133.         }
  134.  
  135.     if ( !GetLongword( in_file, &longword ) ) 
  136.         {
  137.         eolib = TRUE;
  138.         return( TRUE );
  139.         }
  140.  
  141.     mod_buf[buf_count++] = longword;
  142.     current_long         = longword;
  143.     return( TRUE );
  144.     }
  145.     
  146.  
  147.     
  148.     
  149. read_lib_module()
  150.     {
  151.     /* 
  152.       Read in the next module from the library, if one exists, and 
  153.       stick all the XDEF symbols in the symbol list. Return either
  154.       the number of longwords in the module or 0 to indicate the 
  155.       end of the library.
  156.     */
  157.  
  158.     clear_sym_list();        /* clean out prev sym list */
  159.     sym_ptr          = sym_list_head;    /* Reset sym ptr */            
  160.     symbol_offset = 0;        /* Clear offset of symbol hunk ptr */
  161.     buf_count     = 0;        /* Nothing in the buffer... */
  162.     eolib          = 0;        /* Not at end-of-library yet */
  163.  
  164.     while( get_and_put() )
  165.         {
  166.         if ( eolib )
  167.             return( 0 );
  168.  
  169.         switch( current_long )
  170.             {
  171.             case HUNK_UNIT      :
  172.             case HUNK_NAME      : get_named_hunk();
  173.                        break;
  174.  
  175.             case HUNK_CODE      :
  176.             case HUNK_DATA      : get_code_data_debug_hunk();
  177.                        break;
  178.  
  179.             case HUNK_DEBUG  : if ( !symbol_offset )
  180.                            symbol_offset = buf_count-1;
  181.                 
  182.                             get_code_data_debug_hunk();
  183.                        break;
  184.  
  185.             case HUNK_BSS       : get_and_put();
  186.                        break;
  187.  
  188.             case HUNK_RELOC32:
  189.             case HUNK_RELOC16:
  190.             case HUNK_RELOC8 : get_reloc_hunk();
  191.                        break;
  192.  
  193.             case HUNK_SYMBOL : get_symbol_hunk();
  194.                        break;
  195.  
  196.             case HUNK_EXT     : get_public_symbols();
  197.                        break;
  198.  
  199.             case HUNK_END     : return( buf_count );
  200.                        break;
  201.             }
  202.         }
  203.     }
  204.  
  205.  
  206.  
  207. get_public_symbols()
  208.     {
  209.     /* 
  210.       Stick the XDEF symbols in the HUNK_EXT hunk in the symbol
  211.       list.
  212.     */
  213.  
  214.     unsigned long    size, ref_count, name_buf[10], i;
  215.     unsigned char    type;
  216.  
  217.     get_and_put(); /* read first symbol data unit */
  218.     
  219.     while ( current_long )     /* repeat til 0 */
  220.         {
  221.         size = current_long & 0xffffff;    /* Strip type field */
  222.         type = current_long >> 24;
  223.  
  224.         for ( i = 0; i < size; i++ )
  225.             {
  226.             get_and_put();
  227.             name_buf[i] = current_long;
  228.             }
  229.         name_buf[size] = '\0';  /* null term the string */
  230.  
  231.         switch( type )
  232.         {
  233.  
  234.         case ext_symb    : 
  235.         case ext_def    :
  236.         case ext_abs    :
  237.                   /* Eat the Offset longword */
  238.         case ext_res    : get_and_put();
  239.                   add_symbol( name_buf );
  240.                     break;
  241.  
  242.         /* 
  243.           The rest of this switch statement just eats the infor-
  244.           mation contained in the hunk because we don't need it.
  245.         */
  246.  
  247.         case ext_ref32  : goto skip;
  248.  
  249.         case ext_common    : /* The next field is the size field */
  250.                   get_and_put();
  251.  
  252. skip:        case ext_ref16    : /* Eat the ref count and the ref
  253.                      offset fields */
  254.  
  255.         case ext_ref8    : get_and_put();    
  256.                   ref_count = current_long;
  257.  
  258.                   for ( i = 0; i < ref_count; i++ )
  259.                       get_and_put();
  260.                   break;
  261.         }    
  262.  
  263.         get_and_put();
  264.         }
  265.     }
  266.  
  267.  
  268.  
  269. get_named_hunk()
  270.  
  271.     {
  272.     /*
  273.       Read in the named hunk (HUNK_NAME/HUNK_UNIT).
  274.     */
  275.     register int i;
  276.  
  277.     get_and_put();
  278.     temp = current_long;
  279.  
  280.     for( i = 0; i < temp; i++ )
  281.         {
  282.         get_and_put();
  283.         }
  284.     }
  285.  
  286.  
  287. get_code_data_debug_hunk()
  288.     {
  289.     /* 
  290.       Read in the CODE, DATA or DEBUG hunk.
  291.     */
  292.     register int i;
  293.  
  294.     get_and_put();
  295.     temp = current_long;
  296.  
  297.     for ( i = 0; i < temp; i++ )
  298.         get_and_put();        
  299.     }
  300.  
  301.  
  302. get_reloc_hunk()
  303.     {
  304.     /* 
  305.       Eat the info in the relocxx hunks.
  306.     */
  307.     register int i;
  308.     unsigned long relocs;
  309.  
  310.     get_and_put();
  311.  
  312.     while( current_long )
  313.         {
  314.         relocs = current_long;
  315.  
  316.         get_and_put();
  317.  
  318.         for( i = 0; i < relocs; i++ )
  319.             get_and_put();
  320.  
  321.         get_and_put();
  322.         }
  323.     }
  324.  
  325.  
  326. get_symbol_hunk()
  327.     {
  328.     /* 
  329.       Chew through the symbol hunk.
  330.     */
  331.  
  332.     unsigned long    ext_id;
  333.     unsigned int    i;
  334.     
  335.     /*
  336.       Mark the place where the symbol hunk begins.
  337.     */
  338.     symbol_offset = buf_count-1;    
  339.  
  340.     get_and_put();
  341.     
  342.     while ( current_long )
  343.         {
  344.         ext_id = current_long;
  345.  
  346.         for ( i = 0; i <= ext_id; i++ )
  347.             get_and_put();
  348.  
  349.         get_and_put();
  350.         }
  351.     }        
  352.  
  353.  
  354. clear_sym_list()
  355.     {
  356.     /* 
  357.       Destroy the symbol list if one exists, but leave the head node 
  358.       intact for future use and abuse.
  359.     */
  360.  
  361.     struct symbol    *p, *q = sym_list_head->next;
  362.  
  363.     while ( q )
  364.         {
  365.         p = q->next;
  366.         
  367.         FreeMem( q, (long) sizeof( struct symbol ) );
  368.         q = p;
  369.         }        
  370.  
  371.     sym_list_head->next = NULL;
  372.     }
  373.  
  374.  
  375. copy_lib( in_file, out_file )
  376. long    in_file, out_file;
  377.     {
  378.     char    *p;
  379.     long    count, Read();
  380.  
  381.     if ( !( p = AllocMem( READBUFSIZE, 0L ) ) )
  382.         {
  383.         my_exit( ENOMEM );
  384.         }
  385.         
  386.         
  387.     while( ( count = Read( in_file, p, READBUFSIZE ) ) )
  388.         {
  389.         Write( out_file, p, count );
  390.         }
  391.  
  392.     FreeMem( p, READBUFSIZE );
  393.     }
  394.